﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using G.Navigate;

namespace G.Reader.View.Message
{
    public partial class ChatPage : GBasePage
    {
        PushService.PushServiceClient client = new PushService.PushServiceClient();
        public ChatPage()
        {
            InitializeComponent();
        }
        int userid;
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            userid = int.Parse(this.NavigationContext.QueryString["userid"]);
            //u = this.NavigationContext.Param as UserInfoService.UserInfo;
            //this.PageTitle.Text = u.NickName;
        }
        private void txtSend_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                this.Focus();
                if (!string.IsNullOrEmpty(txtSend.Text))
                {
                    byte[] body = System.Text.Encoding.UTF8.GetBytes(txtSend.Text);
                    if (body.Length > 1012)
                    {
                        MessageBox.Show("消息内容太多，无法发送");
                        return;
                    }
                    byte[] buffer = new byte[body.Length + 12];
                    BitConverter.GetBytes(GloabalData.Instance.User.Id).CopyTo(buffer, 0);//from id
                    BitConverter.GetBytes(userid).CopyTo(buffer, 4);//recive id
                    BitConverter.GetBytes(0).CopyTo(buffer, 8);//type
                    body.CopyTo(buffer, 12);//body
                    client.PushMessage_WP7_RawAsync(userid, buffer, new Dictionary<string, string>());
                    this.txtSend.Text = "";
                }
            }
        }
    }
}